﻿@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Index.cshtml";
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>上传</title>
    <link href="~/assets/css/bootstrap-duallistbox.min.css" rel="stylesheet" />
</head>
<body>
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h3 class="smaller lighter blue no-margin"><i class="glyphicon glyphicon-plus-sign"></i>&nbsp;上传</h3>
    </div>

    <div class="modal-body">
        <iv class="form-horizontal" role="form">
            <!-- #section:elements.form -->
            <div class="form-group">
                <div class="col-sm-2">
                </div>
                <div class="col-sm-6">
                    <input type="file" id="file" name="upfil" style="border:1px;" placeholder="请选择上传文件" />
                </div>
                <div class="col-sm-2">
                    <input name="submit" type="button" id="upfile" style="height:22.8px; font-size:10px;" value="导入" />
                </div>
                <div class="col-sm-2">
                </div>
            </div>

    </div>

    <div class="modal-footer">
        <button class="btn btn-sm pull-right" data-dismiss="modal">
            <i class="ace-icon fa fa-times"></i>
            关闭
        </button>

    </div>
    <script src="~/Content/js/jquery.bootstrap-duallistbox.min.js"></script>
    <script src="~/Content/js/jquery-ui.custom.min.js"></script>
    <script src="~/Content/js/bootbox.js"></script>
    <script src="~/Content/js/jquery.form.js"></script>
    <script src="~/Content/js/validate/jquery.validate.min.js"></script>
    <script src="~/Content/validateForm.js"></script>
    <script>
        $(function () {
            $("#upfile")
                .click(function () {
                    var formData = new FormData();
                    var filename = $("#file")[0].files[0];
                    if (filename == null || filename == "") {
                        bootbox.alert("请选择要导入的文件");
                        return;
                    }
                    if (filename != null) {
                        formData.append('file', $("#file")[0].files[0]); //将文件转成二进制形式
                        $.ajax({
                            type: "post",
                            url: "/Resident/UploadFile",//"/文件名/控制器名/控制器内方法"
                            async: false,
                            contentType: false, //这个一定要写
                            processData: false, //这个也一定要写，不然会报错
                            data: formData,
                            dataType: 'json', //返回类型，有json，text，HTML。
                            success: function (data) {
                                alert(data.success);
                            },
                            error: function (xhr, textStatus) {
                                bootbox.alert("导入失败");
                            }
                        });
                    } else {

                    }
                });
        })

    </script>
</body>
</html>
